package org.eenie.kdassistant.ui.scan;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.eenie.kdassistant.R;
import org.eenie.kdassistant.injector.components.AppComponent;
import org.eenie.kdassistant.net.common.ExpressStateListener;
import org.eenie.kdassistant.ui.ExpressEntity;

import java.util.List;


/**
 * Created by Eenie on 2017/8/4.
 * Email: eeniework@gmail.com
 * Des:
 */

public class ScanResAdapter extends BaseQuickAdapter<ExpressEntity, BaseViewHolder> implements ExpressStateListener,
        BaseQuickAdapter.OnItemLongClickListener {
    private AppComponent mAppComponent;

    public ScanResAdapter(@Nullable List<ExpressEntity> data, AppComponent appComponent) {
        super(R.layout.item_scan_res, data);
        mAppComponent = appComponent;
    }

    @Override
    protected void convert(BaseViewHolder helper, ExpressEntity item) {
        String com = "圆";
        if (item.getType() == -1) {
            com = "未";
            helper.setBackgroundRes(R.id.tv_express_com, R.drawable.shape_circle_bg_black);

        } else if (item.getType() == 0) {
            com = "圆";
            helper.setBackgroundRes(R.id.tv_express_com, R.drawable.shape_circle_bg);
        } else if (item.getType() == 1) {
            com = "中";
            helper.setBackgroundRes(R.id.tv_express_com, R.drawable.shape_circle_bg_blue);
        } else if (item.getType() == 2) {
            com = "申";
            helper.setBackgroundRes(R.id.tv_express_com, R.drawable.shape_circle_bg_yellow);
        } else if (item.getType() == 3) {
            com = "韵";
            helper.setBackgroundRes(R.id.tv_express_com, R.drawable.shape_circle_bg_yellow);
        }

        if (item.getState() == 3) {
            helper.setTextColor(R.id.tv_express_state_des, Color.parseColor("#1DBB51"));
        } else if (item.getState() == 4) {
            helper.setTextColor(R.id.tv_express_state_des, Color.RED);
        } else {
            helper.setTextColor(R.id.tv_express_state_des, Color.GRAY);
        }

        helper.setText(R.id.tv_express_num, item.getExpress());

        helper.setText(R.id.tv_express_com, com);

        helper.setText(R.id.tv_express_state_des, item.getState_des());

        item.setAppComponent(mAppComponent);
        item.setListener(this);
        setOnItemLongClickListener(this);

    }


    public void refreshItem(ExpressEntity entity) {
        int pos = getData().indexOf(entity);
        if (pos > -1) {
            notifyItemChanged(pos);
        } else {
            notifyDataSetChanged();
        }
    }


    @Override
    public void addData(@IntRange(from = 0L) int position, @NonNull ExpressEntity data) {
        super.addData(position, data);
        data.setAppComponent(mAppComponent);
        data.setListener(this);

        if (data.getType() == -1) {
            data.fetchCom();
        } else {
            data.upload();
        }


    }

    @Override
    public void onFetchCom(ExpressEntity entity) {
        refreshItem(entity);
    }

    @Override
    public void before(ExpressEntity entity) {
        refreshItem(entity);
    }

    @Override
    public void onSuccess(ExpressEntity entity) {
        refreshItem(entity);
    }

    @Override
    public void onFailure(ExpressEntity entity) {
        refreshItem(entity);
    }


    private void showDialog(final ExpressEntity entity, final int position) {
        AlertDialog dialog = new AlertDialog.Builder(mContext)
                .setItems(new String[]{"重新查询快递公司", "上传至圆通", "上传至中通", "上传至申通", "上传至韵达", "删除该记录"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                entity.fetchCom();
                                break;
                            case 1:
                                entity.setType(0);
                                entity.upload();
                                break;
                            case 2:
                                entity.setType(1);
                                entity.upload();
                                break;
                            case 3:
                                entity.setType(2);
                                entity.upload();
                                break;
                            case 4:
                                entity.setType(3);
                                entity.upload();
                                break;
                            case 5:
                                mAppComponent.getDaoSession().getExpressEntityDao().delete(entity);
                                remove(position);
                                break;
                        }
                    }
                })
                .create();


        dialog.show();

    }


    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        ExpressEntity entity = (ExpressEntity) adapter.getData().get(position);
        showDialog(entity, position);
        return true;
    }
}
